﻿using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace AzureToolkit
{
    public class AzureStorageFactory : AzureToolkit.IAzureStorageFactory
    {
        private CloudStorageAccount account;

        public AzureStorageFactory()
        {
        }

        public AzureStorageFactory(CloudStorageAccount account)
        {
            this.account = account;
        }

        public CloudStorageAccount Account
        {
            get
            {
                if (this.account == null)
                {
                    this.account = CloudConfiguration.GetStorageAccount();
                }
                return this.account;
            }
            set
            {
                this.account = value;
            }
        }

        public static CloudStorageAccount DefaultAccount
        {
            get
            {
                return CloudConfiguration.GetStorageAccount();
            }
        }

        public IAzureBlobContainer<byte[]> GetFileContainer(string containerName, string containerType)
        {
            return new FilesBlobContainer(this.Account, containerName, containerType);
        }

        public IAzureBlobContainer<byte[]> GetFileContainer(string containerName, string containerType, BlobContainerPermissions blobContainerPermissions)
        {
            return new FilesBlobContainer(this.Account, containerName, containerType, blobContainerPermissions);
        }

        public IAzureBlobContainer<T> GetEntitiesContainer<T>(string containerName)
        {
            return new EntitiesBlobContainer<T>(this.Account, containerName);
        }

        public IAzureQueue<T> GetQueue<T>(string queueName) where T : IAzureQueueMessage
        {
            return new AzureQueue<T>(this.Account, queueName);
        }

        public IAzureQueue<T> GetQueue<T>(string queueName, TimeSpan visibilityTimeout) where T : IAzureQueueMessage
        {
            return new AzureQueue<T>(this.Account, queueName, visibilityTimeout);
        }

        public IAzureTable<T> GetTable<T>(string tableName) where T : TableServiceEntity
        {
            return new AzureTable<T>(this.Account, tableName);
        }

    }
}
